var express = require('express');
var router = express.Router();
var MongoClient=require('mongodb').MongoClient;
var DB_CONN_STR='mongodb://127.0.0.1:27017/mui';

router.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.get('/user',function(req,res){
	var usersname=req.query.usersname;
	var upwd=req.query.userpwd;
	MongoClient.connect(DB_CONN_STR,function(err,db){
		if(err){
			console.log(err);
		}else{
			var coll=db.collection('users');
			var data = {
				'usersname':usersname,
				'password':upwd
			}
			
			coll.insertOne(data);
		}
		db.close();
	})

})

module.exports = router;
